package com.dm.wallpaper.library.items;

import com.dm.wallpaper.library.preferences.Preferences;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/*
 * Wallpaper Board
 *
 * Copyright (c) 2017 Dani Mahardhika
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class PopupItem {

    private final String mTitle;
    private int mIcon;
    private boolean mShowCheckbox;
    private boolean mCheckboxValue;
    private boolean mIsSelected;
    private Type mType;

    public PopupItem(String title) {
        mTitle = title;
        mShowCheckbox = false;
        mCheckboxValue = false;
        mIsSelected = false;
    }

    //TODO DrawableRes
    public PopupItem setIcon(int icon) {
        mIcon = icon;
        return this;
    }

    public PopupItem setShowCheckbox(boolean showCheckbox) {
        mShowCheckbox = showCheckbox;
        return this;
    }

    public PopupItem setCheckboxValue(boolean checkboxValue) {
        mCheckboxValue = checkboxValue;
        return this;
    }

    public PopupItem setSelected(boolean selected) {
        mIsSelected = selected;
        return this;
    }

    public PopupItem setType(Type type) {
        mType = type;
        return this;
    }

    public String getTitle() {
        return mTitle;
    }

    //TODO DrawableRes
    public int getIcon() {
        return mIcon;
    }

    public boolean isShowCheckbox() {
        return mShowCheckbox;
    }

    public boolean getCheckboxValue() {
        return mCheckboxValue;
    }

    public boolean isSelected() {
        return mIsSelected;
    }

    public Type getType() {
        return mType;
    }

    public enum Type {
        WALLPAPER_CROP,
        HOMESCREEN,
        LOCKSCREEN,
        HOMESCREEN_LOCKSCREEN,
        DOWNLOAD,
        SORT_LATEST,
        SORT_OLDEST,
        SORT_NAME,
        SORT_RANDOM,
    }

    public static List<PopupItem> getApplyItems(Context context) {
        List<PopupItem> items = new ArrayList<>();

//TODO 加入操作item

//        try {
//            items.add(new PopupItem(context.getResourceManager().getElement(ResourceTable.String_menu_wallpaper_crop).getString())
//                    .setType(Type.LOCKSCREEN)
//                    .setIcon(ResourceTable.Graphic_ic_toolbar_lockscreen));
//
//            items.add(new PopupItem(context.getResourceManager().getElement(ResourceTable.String_menu_apply_homescreen).getString())
//                    .setType(Type.HOMESCREEN)
//                    .setIcon(ResourceTable.Graphic_ic_toolbar_homescreen));
//
//            items.add(new PopupItem(context.getResourceManager().getElement(ResourceTable.String_menu_apply_homescreen_lockscreen).getString())
//                    .setType(Type.HOMESCREEN_LOCKSCREEN)
//                    .setIcon(ResourceTable.Graphic_ic_toolbar_homescreen_lockscreen));
//
//            if (context.getResourceManager().getElement(ResourceTable.Boolean_enable_wallpaper_download).getBoolean()) {
//                items.add(new PopupItem(context.getResourceManager().getElement(ResourceTable.String_menu_save).getString())
//                        .setType(Type.DOWNLOAD)
//                        .setIcon(ResourceTable.Graphic_ic_toolbar_download));
//            }
//        } catch (Exception e) {
//            Log.i("PopupItem.getSortItems([context, selection]):163                  报错了:" + e.getMessage());
//        }

        return items;
    }

    public static List<PopupItem> getSortItems(Context context, boolean selection) {
        List<PopupItem> items = new ArrayList<>();
//        try {
//            items.add(new PopupItem(context.getResourceManager().getElement(ResourceTable.String_menu_sort_latest).getString())
//                    .setType(Type.SORT_LATEST)
//                    .setIcon(ResourceTable.Graphic_ic_toolbar_sort_latest));
//
//            items.add(new PopupItem(context.getResourceManager().getElement(ResourceTable.String_menu_sort_oldest).getString())
//                    .setType(Type.SORT_OLDEST)
//                    .setIcon(ResourceTable.Graphic_ic_toolbar_sort_oldest));
//
//            items.add(new PopupItem(context.getResourceManager().getElement(ResourceTable.String_menu_sort_name).getString())
//                    .setType(Type.SORT_NAME)
//                    .setIcon(ResourceTable.Graphic_ic_toolbar_sort_name));
//
//            items.add(new PopupItem(context.getResourceManager().getElement(ResourceTable.String_menu_sort_random).getString())
//                    .setType(Type.SORT_RANDOM)
//                    .setIcon(ResourceTable.Graphic_ic_toolbar_sort_random));
//        } catch (Exception e) {
//            e.printStackTrace();
//            Log.i("PopupItem.getSortItems([context, selection]):163                  报错了:" + e.getMessage());
//        }

        if (selection) {
            Type type = Preferences.get().getSortBy();
            int order = Preferences.get().getSortByOrder(type);
            if (order >= 0 && order < items.size()) {
                {
                    items.get(order).setSelected(true);
                }
            }
        }
        return items;
    }
}
